
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class aulaSwing extends JFrame {

    JPanel Jpainel = new JPanel();
    JButton JbAmarelo = new JButton("Amarelo");
    JButton JbVermelho = new JButton("Vermelho");

    public aulaSwing() {
        super("Aula");

        setVisible(true);
        setResizable(false);
        setSize(300, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);


        JbVermelho.addMouseListener(new ManipuladorEvento(Color.red));
        JbAmarelo.addMouseListener(new ManipuladorEvento(Color.yellow));


        Jpainel.setBackground(Color.gray);
        Jpainel.add(JbAmarelo);
        Jpainel.add(JbVermelho);
        Jpainel.add(new JLabel("cor clicada :"));

        this.setContentPane(Jpainel);
    }

    public static void main(String args[]) {
        aulaSwing aulaSwing = new aulaSwing();
    }

    private class ManipuladorEvento extends MouseAdapter {
        Color cor;

        @Override
        public void mouseClicked(MouseEvent e) {

            Jpainel.setBackground(cor);
        }

        public ManipuladorEvento(Color cor) {
            this.cor = cor;
        }
    }
}
